﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VideoClassLibrary.BusinessLogicLayer;
using System.Data;
using VideoClassLibrary.Extension;
using VideoClassLibrary.Model;

public partial class CourseManagement_QuantityAddCourseInfo : AdminUserBase
{
    public int CurrentTraingClassId = 0;
    public string CurrentTraingClassName = string.Empty;
    public string Rtn = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!string.IsNullOrEmpty(Request["T"]))
        {
            Rtn = Request["T"];
        }

         
        if (!string.IsNullOrEmpty(Request.QueryString["TClassId"]))
        {
            CurrentTraingClassId = int.Parse(Request.QueryString["TClassId"].ToString());
            CurrentTraingClassName = Request.QueryString["TClassName"].ToString();
            SetPageData();
        }
        if (!IsPostBack)
        {
            SetPageSchoolName();

        }
    }
    private void SetPageData()
    {
        //HiddenField hidTrainingClassId = null;
        //TextBox txtTrainingClassName = null;
        //for (int i = 1; i <= 10; i++)
        //{
        //    hidTrainingClassId = (HiddenField)this.FindControl("HidTrainClassID" + i);
        //    txtTrainingClassName = (TextBox)this.FindControl("txtTrainClassName" + i);
        //    hidTrainingClassId.Value = CurrentTraingClassId.ToString();
        //    txtTrainingClassName.Text = CurrentTraingClassName;
        //    txtTrainingClassName.Enabled = false;
        //}
    }


    
    //获取当前所属学校
    private void SetPageSchoolName()
    {

        int schoolid = this.CurrentUser.SchoolID;
        TeacherModule ta = new TeacherModule();
        string schoolName = ta.GetSchNameByID(schoolid);
        HidSchoolID.Value = CurrentUser.SchoolID.ToString();
        txtSchoolID.Text = schoolName;
        txtTrainClassName.Text = CurrentTraingClassName;
        HidTrainClassID.Value = CurrentTraingClassId.ToString();
        if (!string.IsNullOrEmpty(CurrentTraingClassName))
        {
            txtTrainClassName.Enabled = false;
        }
        txtSchoolID.Enabled = false;
        //for (int i = 1; i <= 10; i++)
        //{
        //    TextBox textbox = (TextBox)this.FindControl("txtSchoolID" + i);
        //    textbox.Text = schoolName;
        //    textbox.Enabled = false;
        //}

    }
    //private void SetAutoCompleteContextKey()
    //{
    //    for (int i = 1; i <= 10; i++)
    //    {
    //        ((AjaxControlToolkit.AutoCompleteExtender)this.FindControl("AceTrainClassName" + i)).ContextKey = this.SchoolID.ToString();
    //    }
    //}
    //录入数据
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        int failCount = 0;
        for (int i = 1; i <= 10; i++)
        {            
            int schoolid = this.CurrentUser.SchoolID;
            HiddenField hidTrainClassId = (HiddenField)this.FindControl("HidTrainClassID" + i);
            TextBox txtTrainClassName = (TextBox)this.FindControl("txtTrainClassName" + i);
            TextBox textboxVC_name = (TextBox)this.FindControl("txtVC_name" + i);
            TextBox textboxVC_desc = (TextBox)this.FindControl("txtVC_desc" + i);
            TextBox textboxVC_content = (TextBox)this.FindControl("txtcontent" + i);
            TextBox textboxVC_targetUsers = (TextBox)this.FindControl("txttargetUsers" + i);
            Label lblMsg = (Label)this.FindControl("lblMsg" + i);
            if (int.Parse(hidTrainClassId.Value) == 0 || txtTrainClassName.Text == "" || textboxVC_name.Text=="")
            {
                //lblMsg.Text = "填写信息不完整";
                continue;
            }
            V_course cource = new V_course();
            CourseModule cm = new CourseModule();
            cource.TrainClassID = Convert.ToInt32(hidTrainClassId.Value);
            cource.Create_time = DateTime.Now;
            cource.SchoolID = schoolid;
            cource.Update_time = DateTime.Now;
            cource.VC_desc = textboxVC_desc.Text;
            cource.VC_name = textboxVC_name.Text; 
            cource.Vc_content = textboxVC_content.Text;
            cource.Vc_targetUsers = textboxVC_targetUsers.Text;
            int result = cm.AddCourseModel(cource);
            lblMsg.Text=(result > 0?"成功":"添加失败");
            if (result > 0)
            {
                failCount++;
            }
        }
        if (failCount > 0)
        {
            PageHelper.AlterMsgAndRedirect("添加成功！",
                                           Rtn == "L"
                                               ? "ViewTrainingClassList.aspx"
                                               : "SearchCourseList.aspx");
        }else
        {
            JsHelper.Alert("对不起，添加数据有问题，请检查！");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("SearchCourseList.aspx");
    }
}
